package views;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import model.DbHelper;
import presenters.MaintReqPresenter;


public class MaintReqPanel extends JPanel {
	private JTextField textField;
	private String location;
	private JComboBox comboBox;
	private JComboBox comboBox_1;
	private DbHelper myHelper;
	private JButton btnSubmit;
	private MaintReqPresenter myPresenter;

	/**
	 * Create the panel.
	 */
	public MaintReqPanel() {
		setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		add(panel, BorderLayout.CENTER);
		
		Box verticalBox = Box.createVerticalBox();
		panel.add(verticalBox);
		myHelper = new DbHelper();
		
		JLabel lblChooseLocation = new JLabel("Choose Location:");
		verticalBox.add(lblChooseLocation);
		
		comboBox = new JComboBox(myHelper.getLocations());
		verticalBox.add(comboBox);
		comboBox.addActionListener(new MyActionListener());
		
		JLabel lblChooseCar = new JLabel("Choose Car:");
		verticalBox.add(lblChooseCar);
		
		location = String.valueOf(comboBox.getSelectedItem());
		comboBox_1 = new JComboBox(myHelper.getCars(location));
		verticalBox.add(comboBox_1);
		
		JLabel lblBriefDescriptionOf = new JLabel("Brief Description of Problem:");
		verticalBox.add(lblBriefDescriptionOf);
		
		textField = new JTextField();
		verticalBox.add(textField);
		textField.setColumns(10);
		
		btnSubmit = new JButton("SUBMIT REQUEST");
		verticalBox.add(btnSubmit);
		btnSubmit.addActionListener(new MyActionListener());
		
		JLabel lblMaintenanceRequest = new JLabel("Maintenance Request");
		add(lblMaintenanceRequest, BorderLayout.NORTH);
		myPresenter = new MaintReqPresenter(this);
	}
	
	private class MyActionListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent ae) {
			Object o = ae.getSource();
			if (o.equals(btnSubmit)) {
				System.out.println("Submit button registered");
				location = String.valueOf(comboBox.getSelectedItem());
				String model = String.valueOf(comboBox_1.getSelectedItem());
				String desc = textField.getText();
				System.out.println("Location: " +location + "\t model: " + model + "\t Description: " + desc );
				myPresenter.submitRequest(location, model, desc);
			}
			else {
				location = String.valueOf(comboBox.getSelectedItem());
				comboBox_1 = new JComboBox(myHelper.getCars(location));
			}
		}
		
	}

}
